package com.amap.api.col.s;

import android.content.Context;
import android.text.TextUtils;
import com.amap.api.col.s.am;
import com.amap.api.col.s.ao;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.ServiceSettings;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.github.mikephil.charting.utils.Utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: ReverseGeocodingHandler.java */
/* loaded from: classes.dex */
public final class aq extends f<RegeocodeQuery, RegeocodeAddress> {
    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.amap.api.col.s.f, com.amap.api.col.s.e
    public final /* synthetic */ Object a(String str) throws AMapException {
        return c(str);
    }

    public aq(Context context, RegeocodeQuery regeocodeQuery) {
        super(context, regeocodeQuery);
    }

    @Override // com.amap.api.col.s.dz
    public final String b() {
        return m.a() + "/geocode/regeo?";
    }

    private static RegeocodeAddress c(String str) throws AMapException {
        JSONObject optJSONObject;
        RegeocodeAddress regeocodeAddress = new RegeocodeAddress();
        try {
            optJSONObject = new JSONObject(str).optJSONObject("regeocode");
        } catch (JSONException e) {
            n.a(e, "ReverseGeocodingHandler", "paseJSON");
        }
        if (optJSONObject == null) {
            return regeocodeAddress;
        }
        regeocodeAddress.setFormatAddress(v.a(optJSONObject, "formatted_address"));
        JSONObject optJSONObject2 = optJSONObject.optJSONObject("addressComponent");
        if (optJSONObject2 != null) {
            v.a(optJSONObject2, regeocodeAddress);
        }
        regeocodeAddress.setPois(v.c(optJSONObject));
        JSONArray optJSONArray = optJSONObject.optJSONArray("roads");
        if (optJSONArray != null) {
            v.b(optJSONArray, regeocodeAddress);
        }
        JSONArray optJSONArray2 = optJSONObject.optJSONArray("roadinters");
        if (optJSONArray2 != null) {
            v.a(optJSONArray2, regeocodeAddress);
        }
        JSONArray optJSONArray3 = optJSONObject.optJSONArray("aois");
        if (optJSONArray3 != null) {
            v.c(optJSONArray3, regeocodeAddress);
        }
        return regeocodeAddress;
    }

    private String a(boolean z) {
        StringBuilder sb = new StringBuilder();
        sb.append("output=json&location=");
        if (z) {
            sb.append(n.a(((RegeocodeQuery) this.b).getPoint().getLongitude()));
            sb.append(",");
            sb.append(n.a(((RegeocodeQuery) this.b).getPoint().getLatitude()));
        }
        if (!TextUtils.isEmpty(((RegeocodeQuery) this.b).getPoiType())) {
            sb.append("&poitype=");
            sb.append(((RegeocodeQuery) this.b).getPoiType());
        }
        if (!TextUtils.isEmpty(((RegeocodeQuery) this.b).getMode())) {
            sb.append("&mode=");
            sb.append(((RegeocodeQuery) this.b).getMode());
        }
        if (!TextUtils.isEmpty(((RegeocodeQuery) this.b).getExtensions())) {
            sb.append("&extensions=");
            sb.append(((RegeocodeQuery) this.b).getExtensions());
        } else {
            sb.append("&extensions=base");
        }
        sb.append("&radius=");
        sb.append((int) ((RegeocodeQuery) this.b).getRadius());
        sb.append("&coordsys=");
        sb.append(((RegeocodeQuery) this.b).getLatLonType());
        sb.append("&key=");
        sb.append(bw.f(this.e));
        return sb.toString();
    }

    @Override // com.amap.api.col.s.f, com.amap.api.col.s.e
    protected final String a_() {
        return a(true);
    }

    private static ao j() {
        an a = am.a().a("regeo");
        if (a == null) {
            return null;
        }
        return (ao) a;
    }

    @Override // com.amap.api.col.s.e
    protected final am.b e() {
        ao j = j();
        double a = j != null ? j.a() : Utils.DOUBLE_EPSILON;
        am.b bVar = new am.b();
        bVar.a = b() + a(false) + "language=" + ServiceSettings.getInstance().getLanguage();
        if (this.b != 0 && ((RegeocodeQuery) this.b).getPoint() != null) {
            bVar.b = new ao.a(((RegeocodeQuery) this.b).getPoint().getLatitude(), ((RegeocodeQuery) this.b).getPoint().getLongitude(), a);
        }
        return bVar;
    }
}
